#pragma once
#include <stdlib.h>
#include "signup.h"
#include "menu.h"

namespace minesweeper {

	using namespace System;
	using namespace System::ComponentModel;
	using namespace System::Collections;
	using namespace System::Windows::Forms;
	using namespace System::Data;
	using namespace System::Drawing;

	/// <summary>
	/// Summary for Form1
	///
	/// WARNING: If you change the name of this class, you will need to change the
	///          'Resource File Name' property for the managed resource compiler tool
	///          associated with all .resx files this class depends on.  Otherwise,
	///          the designers will not be able to interact properly with localized
	///          resources associated with this form.
	/// </summary>
	public ref class Form1 : public System::Windows::Forms::Form
	{
	public:
		Form1(void)
		{
			InitializeComponent();
			//
			//TODO: Add the constructor code here
			//
		}

	protected:
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		~Form1()
		{
			if (components)
			{
				delete components;
			}
		}
	private: System::Windows::Forms::Label^  label1;
	private: System::Windows::Forms::Label^  label2;
	private: System::Windows::Forms::Label^  label3;
	private: System::Windows::Forms::Label^  label4;
	private: System::Windows::Forms::TextBox^  txt_username;
	private: System::Windows::Forms::TextBox^  txt_password;
	private: System::Windows::Forms::Button^  btn_login;
	private: System::Windows::Forms::Button^  btn_signup;




	private: System::Windows::Forms::Label^  label5;
	protected: 

	protected: 

	protected: 

	private:
		/// <summary>
		/// Required designer variable.
		/// </summary>
		System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		void InitializeComponent(void)
		{
			this->label1 = (gcnew System::Windows::Forms::Label());
			this->label2 = (gcnew System::Windows::Forms::Label());
			this->label3 = (gcnew System::Windows::Forms::Label());
			this->label4 = (gcnew System::Windows::Forms::Label());
			this->txt_username = (gcnew System::Windows::Forms::TextBox());
			this->txt_password = (gcnew System::Windows::Forms::TextBox());
			this->btn_login = (gcnew System::Windows::Forms::Button());
			this->btn_signup = (gcnew System::Windows::Forms::Button());
			this->label5 = (gcnew System::Windows::Forms::Label());
			this->SuspendLayout();
			// 
			// label1
			// 
			this->label1->AutoSize = true;
			this->label1->Font = (gcnew System::Drawing::Font(L"PMingLiU", 12, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(136)));
			this->label1->Location = System::Drawing::Point(95, 46);
			this->label1->Name = L"label1";
			this->label1->Size = System::Drawing::Size(172, 16);
			this->label1->TabIndex = 0;
			this->label1->Text = L"Welcome to MineSweeper";
			// 
			// label2
			// 
			this->label2->AutoSize = true;
			this->label2->Font = (gcnew System::Drawing::Font(L"PMingLiU", 10, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(136)));
			this->label2->Location = System::Drawing::Point(139, 62);
			this->label2->Name = L"label2";
			this->label2->Size = System::Drawing::Size(83, 14);
			this->label2->TabIndex = 1;
			this->label2->Text = L"Login System";
			// 
			// label3
			// 
			this->label3->AutoSize = true;
			this->label3->Font = (gcnew System::Drawing::Font(L"PMingLiU", 10, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(136)));
			this->label3->Location = System::Drawing::Point(23, 125);
			this->label3->Name = L"label3";
			this->label3->Size = System::Drawing::Size(66, 14);
			this->label3->TabIndex = 2;
			this->label3->Text = L"User Name";
			// 
			// label4
			// 
			this->label4->AutoSize = true;
			this->label4->Font = (gcnew System::Drawing::Font(L"PMingLiU", 10, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(136)));
			this->label4->Location = System::Drawing::Point(23, 160);
			this->label4->Name = L"label4";
			this->label4->Size = System::Drawing::Size(57, 14);
			this->label4->TabIndex = 3;
			this->label4->Text = L"Password";
			// 
			// txt_username
			// 
			this->txt_username->Location = System::Drawing::Point(98, 122);
			this->txt_username->Name = L"txt_username";
			this->txt_username->Size = System::Drawing::Size(169, 22);
			this->txt_username->TabIndex = 4;
			this->txt_username->Text = L"Please input your name...";
			this->txt_username->Click += gcnew System::EventHandler(this, &Form1::txt_username_Click);
			// 
			// txt_password
			// 
			this->txt_password->Location = System::Drawing::Point(98, 157);
			this->txt_password->Name = L"txt_password";
			this->txt_password->Size = System::Drawing::Size(169, 22);
			this->txt_password->TabIndex = 5;
			this->txt_password->UseSystemPasswordChar = true;
			// 
			// btn_login
			// 
			this->btn_login->Location = System::Drawing::Point(165, 197);
			this->btn_login->Name = L"btn_login";
			this->btn_login->Size = System::Drawing::Size(75, 23);
			this->btn_login->TabIndex = 6;
			this->btn_login->Text = L"Login";
			this->btn_login->UseVisualStyleBackColor = true;
			this->btn_login->Click += gcnew System::EventHandler(this, &Form1::btn_login_Click);
			// 
			// btn_signup
			// 
			this->btn_signup->Location = System::Drawing::Point(246, 197);
			this->btn_signup->Name = L"btn_signup";
			this->btn_signup->Size = System::Drawing::Size(75, 23);
			this->btn_signup->TabIndex = 7;
			this->btn_signup->Text = L"Sign Up";
			this->btn_signup->UseVisualStyleBackColor = true;
			this->btn_signup->Click += gcnew System::EventHandler(this, &Form1::btn_signup_Click);
			// 
			// label5
			// 
			this->label5->AutoSize = true;
			this->label5->Font = (gcnew System::Drawing::Font(L"PMingLiU", 7, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(136)));
			this->label5->Location = System::Drawing::Point(244, 264);
			this->label5->Name = L"label5";
			this->label5->Size = System::Drawing::Size(90, 10);
			this->label5->TabIndex = 8;
			this->label5->Text = L"Copyright by Team 1";
			// 
			// Form1
			// 
			this->AutoScaleDimensions = System::Drawing::SizeF(6, 12);
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
			this->ClientSize = System::Drawing::Size(346, 283);
			this->Controls->Add(this->label5);
			this->Controls->Add(this->btn_signup);
			this->Controls->Add(this->btn_login);
			this->Controls->Add(this->txt_password);
			this->Controls->Add(this->txt_username);
			this->Controls->Add(this->label4);
			this->Controls->Add(this->label3);
			this->Controls->Add(this->label2);
			this->Controls->Add(this->label1);
			this->MaximizeBox = false;
			this->MaximumSize = System::Drawing::Size(354, 317);
			this->MinimumSize = System::Drawing::Size(354, 317);
			this->Name = L"Form1";
			this->StartPosition = System::Windows::Forms::FormStartPosition::CenterScreen;
			this->Text = L"MineSweeper";
			this->ResumeLayout(false);
			this->PerformLayout();

		}
#pragma endregion
	
private: System::Void txt_username_Click(System::Object^  sender, System::EventArgs^  e) {
			  if (this->txt_username->Text == "Please input your name..." ){
					 this->txt_username->Text = "";
				 }
		 }
private: System::Void btn_login_Click(System::Object^  sender, System::EventArgs^  e) {
			 if (check_exist()){
				 if(check_password()){
					/*player->setpassword(this->txt_password->Text);
					player->setusername(this->txt_username->Text);*/
					this->Hide();
					menu ^form = gcnew menu();
					form->ShowDialog();
					Close();
				 }
			 }
		 }
public: bool check_password(){
		try
        {
			String^ path2 = this->txt_username->Text + ".txt";
            StreamReader^ sr = gcnew StreamReader(path2);
            String^ line;
            line = sr->ReadLine();
			if (line == this->txt_password->Text){
			return true;
			}
            sr->Close();
        }
        catch (Exception^ e)
        {
            Console::WriteLine("The file could not be read:check_password");
            Console::WriteLine(e->Message);
        }
		String^ message = "Password doesnot match!";
		String^ title = "Error Message";
		MessageBox::Show(message,title,MessageBoxButtons::OK);
		clear();
		return false;
}
public: bool check_exist(){
		try
        {
            StreamReader^ sr = gcnew StreamReader("UserList.txt");
            String^ line;
            while ((line = sr->ReadLine()) != nullptr)
            {
				if (line == this->txt_username->Text ){
				return true;
				}
            }
            sr->Close();
        }
        catch (Exception^ e)
        {
            Console::WriteLine("The file could not be read:check_exist");
            Console::WriteLine(e->Message);
        }
			String^ message = "Username doesnot Existed!";
			String^ title = "Error Message";
			MessageBox::Show(message,title,MessageBoxButtons::OK);
			clear();
			return false;
		
	}
public: void clear(){
			this->txt_password->Text="";
			this->txt_username->Text="";
}
private: System::Void btn_signup_Click(System::Object^  sender, System::EventArgs^  e) {
	    signup ^form2 = gcnew signup();
			form2->ShowDialog();
		
		 }


};
}

